<template>
  <div class="app-container">
    <t-form
      :data="queryParams"
      ref="queryRef"
      layout="inline"
      v-show="showSearch"
      label-width="68px"
      class="mb10"
    >
      <t-form-item label="任务名称" name="jobName">
        <t-input
          v-model="queryParams.jobName"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="任务组名" name="jobGroup">
        <t-select
          v-model="queryParams.jobGroup"
          placeholder="请选择任务组名"
          clearable
          style="width: 240px"
        >
          <t-option
            v-for="dict in sys_job_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item label="执行状态" name="status">
        <t-select
          v-model="queryParams.status"
          placeholder="请选择执行状态"
          clearable
          style="width: 240px"
        >
          <t-option
            v-for="dict in sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item label="执行时间" style="width: 308px">
        <t-date-range-picker v-model="dateRange" allow-input clearable />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:job:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          @click="handleClean"
          v-hasPermi="['monitor:job:remove']"
          >清空
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="warning"
          @click="handleExport"
          v-hasPermi="['monitor:job:export']"
          >导出
          <template #icon>
            <t-icon name="download" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button theme="warning" @click="handleClose"
          >关闭
          <template #icon>
            <t-icon name="close" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      v-loading="loading"
      :data="jobLogList"
      row-key="jobLogId"
      :columns="columns"
      @select-change="handleSelectionChange"
    >
      <template #jobGroup="scope">
        <dict-tag :options="sys_job_group" :value="scope.row.jobGroup" />
      </template>
      <template #status="scope">
        <dict-tag :options="sys_common_status" :value="scope.row.status" />
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
      <template #action="scope">
        <t-button
          variant="text"
          theme="primary"
          @click="handleView(scope.row)"
          v-hasPermi="['monitor:job:query']"
          >详细
          <template #icon>
            <t-icon name="browse" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 调度日志详细 -->
    <t-dialog
      title="调度日志详细"
      v-model:visible="open"
      width="700px"
      attach="body"
    >
      <t-form :data="form" label-width="100px">
        <t-row>
          <t-col :span="6">
            <t-form-item label="日志序号：">{{ form.jobLogId }}</t-form-item>
            <t-form-item label="任务名称：">{{ form.jobName }}</t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="任务分组：">{{ form.jobGroup }}</t-form-item>
            <t-form-item label="执行时间：">{{ form.createTime }}</t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="调用方法：">{{
              form.invokeTarget
            }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="日志信息：">{{ form.jobMessage }}</t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="执行状态：">
              <div v-if="form.status == 0">正常</div>
              <div v-else-if="form.status == 1">失败</div>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="异常信息：" v-if="form.status == 1">{{
              form.exceptionInfo
            }}</t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="default" @click="open = false">关 闭</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup name="JobLog">
import { getJob } from "@/api/monitor/job";
import { listJobLog, delJobLog, cleanJobLog } from "@/api/monitor/jobLog";

const { proxy } = getCurrentInstance();
const { sys_common_status, sys_job_group } = proxy.useDict(
  "sys_common_status",
  "sys_job_group"
);

const jobLogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);
const route = useRoute();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dictName: undefined,
    dictType: undefined,
    status: undefined,
  },
});

const columns = ref([
  {
    colKye: "row-select",
    type: "multiple",
    title: "选择",
    width: 50,
    align: "center",
  },
  { colKye: "jobLogId", title: "日志序号", width: 100, align: "center" },
  {
    colKye: "jobName",
    title: "任务名称",
    width: 100,
    align: "center",
    ellipsis: true,
  },
  {
    colKye: "jobGroup",
    title: "任务组名",
    width: 100,
    align: "center",
    ellipsis: true,
  },
  {
    colKye: "invokeTarget",
    title: "调用目标字符串",
    width: 100,
    align: "center",
    ellipsis: true,
  },
  {
    colKye: "jobMessage",
    title: "日志信息",
    width: 100,
    align: "center",
    ellipsis: true,
  },
  { colKye: "status", title: "执行状态", width: 100, align: "center" },
  { colKye: "createTime", title: "执行时间", width: 100, align: "center" },
  { colKye: "action", title: "操作", width: 100, align: "center" },
]);

const { queryParams, form, rules } = toRefs(data);

/** 查询调度日志列表 */
function getList() {
  loading.value = true;
  listJobLog(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      jobLogList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}
// 返回按钮
function handleClose() {
  const obj = { path: "/monitor/job" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  ids.value = selectedRowKeys;
  multiple.value = !selectedRowKeys.length;
}
/** 详细按钮操作 */
function handleView(row) {
  open.value = true;
  form.value = row;
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除调度日志编号为"' + ids.value + '"的数据项?')
    .then(function () {
      return delJobLog(ids.value);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有调度日志数据项?")
    .then(function () {
      return cleanJobLog();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "monitor/jobLog/export",
    {
      ...queryParams.value,
    },
    `job_log_${new Date().getTime()}.xlsx`
  );
}

(() => {
  const jobId = route.params && route.params.jobId;
  if (jobId !== undefined && jobId != 0) {
    getJob(jobId).then((response) => {
      queryParams.value.jobName = response.data.jobName;
      queryParams.value.jobGroup = response.data.jobGroup;
      getList();
    });
  } else {
    getList();
  }
})();

getList();
</script>
